package unit.collisions;
import unit.walkers.SpriteWalker;

import com.golden.gamedev.object.Sprite;
import com.golden.gamedev.object.collision.BasicCollisionGroup;
/*
 * @author Conrad
 * Performs collision operation for two independent walkers
 */
public class WalkerToWalkerCollision extends BasicCollisionGroup implements ICollide{

/*
 *The collided method handles eliminating walkers based on their Level type
 */
	@Override
	public void collided(Sprite walker1, Sprite walker2) {
		int value = compareSprites(walker1, walker2);
		if (value < 0) {
			walker1.setActive(false);
		} else if (value > 0) {
			walker2.setActive(false);
		} else {
			reverseDirection(walker1);
			reverseDirection(walker2);
		}
	}
/*
 * This method transforms Sprites into their respective subclasses and returns an int compareTo value	
 */
	public int compareSprites(Sprite sprite1, Sprite sprite2) {
		SpriteWalker walker1 = getInstance(sprite1.getImage().toString());
		SpriteWalker walker2 = getInstance(sprite1.getImage().toString());
		return((walker1.foodChainRank)-(walker2.foodChainRank));
	}

/*
 * returns a new unspecified SpriteWalker Object
 */
	@SuppressWarnings("unchecked")
	private SpriteWalker getInstance(String walkerType) {
		Class<SpriteWalker> myClass = null;
		try {
			myClass = (Class<SpriteWalker>) Class.forName(walkerType);
		} catch (ClassNotFoundException e1) {
			e1.printStackTrace();
		}
		SpriteWalker walker = null;
		try {
			walker = myClass.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return walker;
	}
	@Override
	public void reverseDirection(Sprite one) {	
		one.setSpeed(one.getHorizontalSpeed()*-1, one.getVerticalSpeed()*-1);
	}
	}